package gae.google.tcserver.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import gae.google.tcserver.db.factory.PMFactory;
import gae.google.tcserver.servlets.RegisterServlet;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
//import java.util.logging.Logger;

/**
 * The <tt>GetFromDB</tt> class returns requested information 
 * from the database
 *
 * @author Lars Friberg
 *
 * @date 2011-01-15
 */
public class GetFromDB {

	// creates an instance of PersistenceManager 
	private static PersistenceManager pm = PMFactory.get().getPersistenceManager();		// creates an instance of PersistenceManager to be able to save the user to the database

    //private static final Logger log = Logger.getLogger(GetFromDB.class.getName());

	/**
	 * get user 
	 * 
	 * @param userKeyId the user key Id
	 * @return User the user
	 */
	public synchronized static User getUser(Key userKeyId) {
		
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		User user = pm.getObjectById(User.class, userKeyId);
		if(!pm.isClosed()) {
			pm.close();
		}
		return user;
	}

	/**
	 * get all users
	 * 
	 * @return users a list of all users
	 */
	public synchronized static List<User> getAllUsers() {
		
		List<User> users = new ArrayList<User>();
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		Extent<User> extent = GetFromDB.pm.getExtent(User.class);
		users = new ArrayList<User>();
		for(User u : extent) {
			users.add(u);
		}
		if(!pm.isClosed()) {
			pm.close();
		}
		return users;
	}
	
	/**
	 * get user by name and password
	 * 
	 * @param userName the user name
	 * @param userPassword the user password
	 * @return User the user
	 */
	@SuppressWarnings("unchecked")
	public synchronized static User getUser(String userName, String userPassword) {
		
		List<User> users = new ArrayList<User>();
		User user = null;
		String[] splitPassword = RegisterServlet.getSaltedHashedSplitPassword(userPassword);
		
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			Query query = pm.newQuery(User.class);
			query.setFilter("userName == userNameParam && userPassword == userPasswordParam && socialSecurityNumber == socialSecurityNumberParam");
			query.declareParameters("String userNameParam, String userPasswordParam, String socialSecurityNumberParam");
			users = (List<User>) query.execute(userName, splitPassword[0], splitPassword[1]);
			if(users != null) {
				if(users.size() >= 1) {
					user = users.get(0);
				}
			}
		}finally {
			if(!pm.isClosed()) {
				pm.close();
			}
		}
		return user;
	}
	
	/**
	 * get team 
	 * 
	 * @param teamKeyId the team key Id
	 * @return Team the team
	 */
	public synchronized static Team getTeam(Key teamKeyId) {
		
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		Team team = pm.getObjectById(Team.class, teamKeyId);
		if(!pm.isClosed()) {
			pm.close();
		}
		return team;
	}

	/**
	 * get all teams
	 * 
	 * @return teams a list of all teams
	 */
	public synchronized static List<Team> getAllTeams() {
		
		List<Team> teams = new ArrayList<Team>();
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		Extent<Team> extent = GetFromDB.pm.getExtent(Team.class);
		teams = new ArrayList<Team>();
		for(Team u : extent) {
			teams.add(u);
		}
		if(!pm.isClosed()) {
			pm.close();
		}
		return teams;
	}

	/**
	 * get team member
	 * 
	 * @param teamKeyId the team key Id
	 * @return TeamMember the team member
	 */
	public synchronized static TeamMember getTeamMember(Key teamMemberKeyId) {
		
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		TeamMember teamMember = pm.getObjectById(TeamMember.class, teamMemberKeyId);
		if(!pm.isClosed()) {
			pm.close();
		}
		return teamMember;
	}
	
	/**
	 * get all month activities
	 * 
	 * @param month the month
	 * @return activities the month activities as a list of key id's
	 */
	@SuppressWarnings("unchecked")
	public synchronized static List<Activity> getActivities(Date fromDate, Date toDate) {
		
		List<Activity> activities = new ArrayList<Activity>();
		
		if(pm.isClosed()){
			pm = PMFactory.get().getPersistenceManager();
		}
		try {
			Query query = pm.newQuery(Activity.class);
			query.setFilter("date >= fromDate");
			query.setFilter("date <= toDate");
			query.declareImports("import java.util.Date");
			query.declareParameters("Date fromDate, Date toDate");
			activities = (List<Activity>) query.execute(fromDate, toDate);
			activities.size();// required, else exception
		}catch (Exception e){
			
		}
		finally {
			if(!pm.isClosed()) {
				pm.close();
			}
		}
		return activities;
	}
}
